//
//  ST_BTCustomEmojiView.swift
//  btvideo
//
//  Created by mac on 2020/7/15.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_BTCustomEmojiView: UIView {
    
    static let height = 267 + BTSafeAreaBottom
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom)
        }
        
        self.addSubview(segment_control)
        segment_control.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom)
            make.height.equalTo(ST_BTCustomEmojiSegmentControl.height)
        }
        
        binding()
    }
    
    private func binding(){
        
        segment_control.selected_closure = {[weak self] idx in
            
            self?.collectionView.setContentOffset(CGPoint.init(x: ScreenWidth*idx.cgFloat, y: 0), animated: true)
            
        }
        
        SCComponets.emoji_component.fetch_collection_emoji().withUnretained(self).subscribe(onNext: { own,list in
            
            
            own.emoji_group_array = list
            own.collectionView.reloadData()
            own.segment_control.tab_image_array = list.map({ (groupModel) -> String in
                return groupModel.bottomImg
            })
            
        }).disposed(by: rx.disposeBag)
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
//    MARK: - Properties
    fileprivate lazy var segment_control:ST_BTCustomEmojiSegmentControl = {
       let control = ST_BTCustomEmojiSegmentControl()
        control.backgroundColor = .white
        return control
    }()
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
      let controlLayout = UICollectionViewFlowLayout()
        controlLayout.scrollDirection = .horizontal
        controlLayout.minimumLineSpacing = 0
        controlLayout.minimumInteritemSpacing = 0
        controlLayout.itemSize = CGSize.init(width: ScreenWidth, height: ST_BTCustomEmojiView.height - BTSafeAreaBottom)
       return controlLayout
   }()
       
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.isPagingEnabled = true
        control.backgroundColor = UIColor.white
        control.delegate = self
        control.dataSource = self

        control.register(cellWithClass: ST_BTEmojiGroupCell.self)
        
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        return control
    }()
    
    var selected_closure:((BTEmojiItemModel)->())?
    
    fileprivate lazy var emoji_group_array = [ST_EmojiGroupModel]()
    
}

//MARK: -
extension ST_BTCustomEmojiView:UICollectionViewDelegate,UICollectionViewDataSource{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return emoji_group_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_BTEmojiGroupCell.self, for: indexPath)
        
        cell.model = emoji_group_array[safe:indexPath.row]
        
        
        cell.selected_closure = {[weak self] emoji in
         
            self?.selected_closure?(emoji)
            
        }
        
        return cell
    }
    
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
                
        
        if !decelerate{
                scrollviewdidEndScroll(scrollView)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        scrollviewdidEndScroll(scrollView)
        
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        

        let currPage = (scrollView.contentOffset.x/ScreenWidth).int
        
        if let obj = self.collectionView.cellForItem(at: IndexPath.init(row: currPage, section: 0)) as? ST_GiftTabOutsideHandleProtocol{
            
            obj.outSideSelectHandle()
        }
    }
    
//    MARK: 停止滚动
    func scrollviewdidEndScroll(_ scrollView:UIScrollView){
        
        let currPage = (scrollView.contentOffset.x/ScreenWidth).int
        
        self.segment_control.selectBtn(index: currPage)
        
    }
}


protocol ST_GiftTabOutsideHandleProtocol {
    
    func outSideSelectHandle()
}
